package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysFlowNode;
import com.ysd.lis.entity.SysFlowNodeEvent;
import com.ysd.lis.mapper.sys.SysFlowNodeEventMapper;
import com.ysd.lis.request.SysFlowParam;
import com.ysd.lis.service.SysFlowNodeEventService;
import com.ysd.lis.service.SysFlowNodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-04-01
 */
@Service
@Slf4j
public class SysFlowNodeEventServiceImpl extends ServiceImpl<SysFlowNodeEventMapper, SysFlowNodeEvent> implements SysFlowNodeEventService {

    @Autowired
    SysFlowNodeEventMapper sysFlowNodeEventMapper;
    @Autowired
    SysFlowNodeService sysFlowNodeService;
    @Override
    public Result findEventList(SysFlowParam param) {
        String nodeId =  param.getNodeId();
        //根据节点获取节点下的事件数据
        LambdaQueryWrapper<SysFlowNodeEvent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysFlowNodeEvent::getNodeId, nodeId).eq(SysFlowNodeEvent::getDelFlag, 0);
        List<SysFlowNodeEvent> flowNodeEventList = sysFlowNodeEventMapper.selectList(lambdaQueryWrapper);
        if(flowNodeEventList.size()>0){
            List<String> collect = flowNodeEventList.stream().map(SysFlowNodeEvent::getNextNode).collect(Collectors.toList());
            List<SysFlowNode> flowNodeList = sysFlowNodeService.listByIds(collect);
            for(SysFlowNodeEvent flowNodeEvent : flowNodeEventList){
                flowNodeEvent.setNextNodeText(flowNodeList.stream().filter(n->n.getId().equals(flowNodeEvent.getNextNode())).findFirst().get().getName());
            }
        }
        return Result.succ(1, "查询成功", flowNodeEventList);
    }

    @Override
    public Result addEvent(SysFlowNodeEvent event) {
        int i = sysFlowNodeEventMapper.insert(event);
        if (i > 0) {
            return Result.succ(1, "新增成功", "");
        }
        return Result.succ(400, "新增失败", "");
    }

    @Override
    public Result editEvent(SysFlowNodeEvent event) {
        int i = sysFlowNodeEventMapper.updateById(event);
        if (i > 0) {
            return Result.succ(1, "修改成功", "");
        }
        return Result.succ(400, "修改失败", "");
    }

    @Override
    public Result deleteEvent(SysFlowNodeEvent event) {
        int i = sysFlowNodeEventMapper.deleteById(event.getId());
        if (i > 0) {
            return Result.succ(1, "删除成功", "");
        }
        return Result.succ(400, "删除失败", "");
    }
}
